// pages/leavelist/leavelist.js
const {
    imgConfig
} = require('../../utils/config.js')
const {
    config
} = require('../../utils/config.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: `${imgConfig}`,
        statusBarHeight: app.globalData.statusBarHeight,
        leaveList: [],
        now:new Date(),
        page:1,
        limit:6
    },
    back: function () {
        wx.navigateBack({});
    },
    /**
     * 销假
     * @param {*} e 
     */
    changeStatus(e) {
        var that = this
        var index = e.target.dataset.index;
        var leaveList = that.data.leaveList
        leaveList[index].status = 3
        var id = that.data.leaveList[index].id; //获取当前选中的请假申请的id

        wx.showModal({
            cancelColor: 'cancelColor',
            content: "确认要销假吗?",
            success(res) {
                if (res.confirm == true) {
                    wx.request({
                        url: `${config}/wechat/wxleave/changeStatus`,
                        data: {
                            id: id
                        },
                        success(resp) {
                            console.log(resp)
                            if (resp.data.code == 0) {
                                
                                wx.showToast({
                                    title: '销假成功',
                                    icon: 'success',
                                    duration:1000,
                                    success(){
                                        that.onLoad()
                                    }
                                })
                               
                            }
                        }
                    })
                } else if (res.cancel == true) {
                    wx.showToast({
                        title: '已取消',
                        icon: 'none',
                        duration: 1000
                    })
                }
            }
        })
    },

    /**
     * 修改申请
     * @param {*} e 
     */
    update(e) {
      console.log(e)
        var that = this
        var index = e.target.dataset.index;
        var id = that.data.leaveList[index].id; //获取当前选中的请假申请的id
        wx.showModal({
            cancelColor: 'cancelColor',
            content: "确认要修改申请吗?",
            success(res) {
                console.log(res)
                if (res.confirm == true) {
                    wx.navigateTo({
                        url: `/pages/leave/leave?id=${id}`,
                    })
                } else if (res.cancel == true) {
                    wx.showToast({
                        title: '已取消',
                        icon: 'none',
                        duration: 1000
                    })
                }
            }
        })
    },


    /**
     * 删除
     * @param {*} e 
     */
    del(e) {
        var that = this
        var index = e.target.dataset.index;
        var currentLeave = that.data.leaveList[index]; //获取当前选中的请假申请
        var leaveList = that.data.leaveList;
        console.log(currentLeave);
        wx.showModal({
            cancelColor: 'cancelColor',
            content: '请确认是否撤回申请?',
            success(resp) {
                console.log(resp)
                if (resp.confirm == true) {
                    wx.request({
                        url: `${config}/wechat/wxleave/delLeave`,
                        data: {
                            id: currentLeave.id
                        },
                        success(res) {
                            leaveList.splice(index, 1);
                            that.setData({
                                leaveList: leaveList
                            });
                            wx.showToast({
                                title: '撤销成功',
                                icon: 'success',
                                duration: 1500
                            })
                        }
                    })
                } else if (resp.cancel == true) {
                    wx.showToast({
                        title: '已取消',
                        icon: 'none',
                        duration: 1000
                    })
                }
            }
        })
    },
    /**
     * 获取请假申请列表
     */
    getLeaveList(){
        var that = this;
        var stuId = app.globalData.userInfo.id;
        wx.request({
            url: `${config}/wechat/wxleave/getLeaveList`,
            data: {
                stuId: stuId,
                page:that.data.page,//分页
                limit:that.data.limit
            },
            success(resp) {
                console.log(resp)
                that.setData({
                    leaveList: resp.data.leaveList.records
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.getLeaveList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.onLoad()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})